package com.sinothk.special.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.base.model.BizUserInfo;
import com.sinothk.special.user.entity.BizUserInfoForHomeVO;
import com.sinothk.special.user.service.IBizUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 用户信息
 * @Author: Liangyt
 * @Date: 2022-03-29
 * @Version: V1.0
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/userBizInfo")
@Slf4j
public class BizUserInfoController extends JeecgController<BizUserInfo, IBizUserInfoService> {
    @Autowired
    private IBizUserInfoService userInfoService;

    /**
     * 分页列表查询
     *
     * @param userInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "用户信息-分页列表查询")
    @ApiOperation(value = "用户信息-分页列表查询", notes = "用户信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BizUserInfo userInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BizUserInfo> queryWrapper = QueryGenerator.initQueryWrapper(userInfo, req.getParameterMap());
        Page<BizUserInfo> page = new Page<BizUserInfo>(pageNo, pageSize);
        IPage<BizUserInfo> pageList = userInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param userInfo
     * @return
     */
    @AutoLog(value = "用户信息-添加")
    @ApiOperation(value = "用户信息-添加", notes = "用户信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BizUserInfo userInfo) {
        userInfoService.save(userInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param userInfo
     * @return
     */
    @AutoLog(value = "用户信息-编辑")
    @ApiOperation(value = "用户信息-编辑", notes = "用户信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BizUserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 编辑-零星更新
     *
     * @param userInfoVo
     * @return
     */
    @AutoLog(value = "用户信息-零星更新")
    @ApiOperation(value = "用户信息-零星更新", notes = "用户信息-零星更新")
    @PutMapping(value = "/editUserBizInfo")
    public Result<BizUserInfoForHomeVO> editUserBizInfo(@RequestBody BizUserInfo userInfoVo) {
        return Result.OK(userInfoService.editUserBizInfo(userInfoVo));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户信息-通过id删除")
    @ApiOperation(value = "用户信息-通过id删除", notes = "用户信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        userInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

//    /**
//     * 批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "用户信息-批量删除")
//    @ApiOperation(value = "用户信息-批量删除", notes = "用户信息-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
//    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.userInfoService.removeByIds(Arrays.asList(ids.split(",")));
//        return Result.OK("批量删除成功!");
//    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户信息-通过id查询")
    @ApiOperation(value = "用户信息-通过id查询", notes = "用户信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BizUserInfo userInfo = userInfoService.getById(id);
        if (userInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(userInfo);
    }

//    /**
//     * 更新-用户状态
//     *
//     * @return
//     */
//    @AutoLog(value = "更新-用户状态")
//    @ApiOperation(value = "更新-用户状态", notes = "更新-用户状态")
//    @PostMapping(value = "/updateUserState")
//    public Result<?> updateUserState() {
//        userInfoService.updateUserState();
//        return Result.OK("添加成功！");
//    }

    /**
     * 用户信息-查询他人用户信息
     *
     * @param userId
     * @return
     */
    @AutoLog(value = "用户信息-查询他人用户信息")
    @ApiOperation(value = "用户信息-查询他人用户信息", notes = "用户信息-查询他人用户信息")
    @GetMapping(value = "/getUserInfoForOther")
    public Result<?> getUserInfoForOther(@RequestParam(name = "userId") String userId) {
        return Result.OK(userInfoService.getUserInfoForOther(userId));
    }
}
